package demo08_ReadWriteLock;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kunkun
 * @className Test01_Problem
 * @Description ...
 * @date 2025/3/6 11:42
 */
public class Test01_Problem {

    public static void main(String[] args) {

        //1. 新建缓存
        MyCacheProblem cache = new MyCacheProblem();

        //2. 写入
        for (int i = 0; i < 5; i++) {
            final int index = i;
            new Thread(()->{
                cache.put(index+"", index);
            },"Thread-"+(index+1)).start();
        }

        //3. 读取
        for (int i = 0; i < 5; i++) {
            final int index = i;
            new Thread(()->{
                cache.get(index+"");
            },"Thread-"+(index+1)).start();
        }

    }


    public static class MyCacheProblem{
        private volatile Map<String, Object> cache = new HashMap<>();

        //读
        public void get(String key) {
            System.out.println(Thread.currentThread().getName() + "读取cache,key="+key);
            cache.get(key);
        }

        //写
        public void put(String key,Object value) {
            System.out.println(Thread.currentThread().getName() + "写入cache--"+key);
            cache.put(key, value);
            System.out.println(Thread.currentThread().getName() + "写入cache完成--"+key);
        }
    }
}
